#include "stm8s_conf.h"
#include "Board.h"






void IO_Init(void)
{
	GPIO_Init(LED1_PORT, LED1_PIN, GPIO_MODE_OUT_PP_HIGH_FAST );//LED1
	GPIO_Init(LED2_PORT, LED2_PIN, GPIO_MODE_OUT_PP_HIGH_FAST );//LED2
	
	GPIO_Init(PWM1_PORT, PWM1_PIN, GPIO_MODE_OUT_PP_LOW_SLOW );//PWM2
	GPIO_Init(PWM2_PORT, PWM2_PIN, GPIO_MODE_OUT_PP_LOW_SLOW );//PWM1
	
	GPIO_WriteLow(LED1_PORT, LED1_PIN);
	GPIO_WriteLow(LED2_PORT, LED2_PIN);


	#if USE_ADC_DET==1
	GPIO_Init(ADCV_PORT, ADCC_PIN,  GPIO_MODE_IN_FL_NO_IT);//ADC VO deceted
	GPIO_Init(ADCC_PORT, ADCC_PIN,  GPIO_MODE_IN_FL_NO_IT);//ADC Cu deceted
	#endif
}



void Timer_Init(void)
{
	#if 0
	TIM2_DeInit();
	TIM2_TimeBaseInit( TIM2_PRESCALER_8 ,
                        		    Delay10ms-1);///10MS 
	TIM2_PrescalerConfig(TIM2_PRESCALER_8,TIM2_PSCRELOADMODE_IMMEDIATE);
	TIM2_ARRPreloadConfig(ENABLE);
	TIM2_ITConfig( TIM2_IT_UPDATE , ENABLE);
	TIM2_UpdateDisableConfig(DISABLE);
	TIM2_UpdateRequestConfig( TIM2_UPDATESOURCE_GLOBAL);
	TIM2_Cmd(ENABLE );///create system timer

	
	TIM4_DeInit();
	TIM4_TimeBaseInit( TIM4_PRESCALER_8 ,Delay200us-1);///100us
	TIM4_PrescalerConfig(TIM4_PRESCALER_8,TIM4_PSCRELOADMODE_IMMEDIATE);
	TIM4_ARRPreloadConfig(ENABLE);
	TIM4_ITConfig( TIM4_IT_UPDATE , ENABLE);
	TIM4_UpdateDisableConfig(DISABLE);
	TIM4_UpdateRequestConfig( TIM4_UPDATESOURCE_GLOBAL);
	TIM4_Cmd(ENABLE );///used for decode handler
	#endif
/*********************************************************************************/
	/* TIM1 Peripheral Configuration */ 
	TIM1_DeInit();  /* Time Base configuration */
	/*
	TIM1_Period = 4095
	TIM1_Prescaler = 0
	TIM1_CounterMode = TIM1_COUNTERMODE_UP
	TIM1_RepetitionCounter = 0
	*/  
	TIM1_TimeBaseInit(TIM1_PRESCALER_8, TIM1_COUNTERMODE_UP, PWMOUT100K, 0);  /* Channel 1, 2,3 and 4 Configuration in PWM mode */

	/*
	TIM1_OCMode = TIM1_OCMODE_PWM2
	TIM1_OutputState = TIM1_OUTPUTSTATE_ENABLE
	TIM1_OutputNState = TIM1_OUTPUTNSTATE_ENABLE
	TIM1_Pulse = CCR1_Val
	TIM1_OCPolarity = TIM1_OCPOLARITY_LOW
	TIM1_OCNPolarity = TIM1_OCNPOLARITY_HIGH
	TIM1_OCIdleState = TIM1_OCIDLESTATE_SET
	TIM1_OCNIdleState = TIM1_OCIDLESTATE_RESET
	*/
	TIM1_OC1Init(TIM1_OCMODE_PWM2, TIM1_OUTPUTSTATE_ENABLE, TIM1_OUTPUTNSTATE_ENABLE, CCR1_Val, TIM1_OCPOLARITY_LOW, TIM1_OCNPOLARITY_HIGH, TIM1_OCIDLESTATE_SET, TIM1_OCIDLESTATE_RESET);  /*TIM1_Pulse = CCR2_Val*/
	TIM1_OC2Init(TIM1_OCMODE_PWM2, TIM1_OUTPUTSTATE_ENABLE, TIM1_OUTPUTNSTATE_ENABLE, CCR2_Val, TIM1_OCPOLARITY_HIGH, TIM1_OCNPOLARITY_LOW, TIM1_OCIDLESTATE_SET, TIM1_OCIDLESTATE_RESET);  /*TIM1_Pulse = CCR3_Val*/

	//GPIO_Init(GPIOD,GPIO_PIN_7,GPIO_MODE_OUT_PP_LOW_FAST);
	//OPT_Init1.OPT2=(OPT_Init1.OPT2|0x10);  /* TIM1 counter enable */
	TIM1_Cmd(ENABLE);  /* TIM1 Main Output Enable */
	
}


void BSP_Init(void)
{
	IO_Init();
	Timer_Init();
	Decode_IoInit(AuthSU_SET);
	/************Paraameter Initialze***************************/
}



void LED_IOCTL(const unsigned char LED_STATUS)
{
	unsigned char LED_STUS=LED_STATUS&LED_MASK_BIT;
	
	if(LED_STUS&LED1_MASK_BIT)
	{
		LED1_HIGH;
	}
	else
	{
		LED1_LOW;
	}
	
	if(LED_STUS&LED2_MASK_BIT)
	{
		LED2_HIGH;
	}
	else
	{
		LED2_LOW;
	}
}

